package com.zhengtoon.mctoon.conv.controller;

import com.bjtoon.framework.entity.ResponseResult;
import com.qitoon.framework.core.exception.BusinessException;
import com.zhengtoon.mctoon.conv.bean.session.OrgSessionInfo;
import com.zhengtoon.mctoon.conv.dto.mybatis.Page;
import com.zhengtoon.mctoon.conv.dto.param.UploadInputParam;
import com.zhengtoon.mctoon.conv.dto.request.*;
import com.zhengtoon.mctoon.conv.dto.response.*;
import com.zhengtoon.mctoon.conv.exception.ConvPhotoExceptionCode;
import com.zhengtoon.mctoon.conv.service.ConvSubjectService;
import com.zhengtoon.mctoon.conv.utils.AppConstant;
import com.zhengtoon.mctoon.conv.utils.SessionUtils;
import com.zhengtoon.mctoon.conv.utils.XssEncodeUtils;
import com.zhengtoon.mctoon.conv.vo.ShiftUpSubjectVo;
import com.zhengtoon.mctoon.conv.vo.SubjectDetailVo;
import com.zhengtoon.mctoon.conv.vo.SubjectManageListVo;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 主题管理
 *
 * @author 144637
 */
@RestController
@RequestMapping("/convSubjectManage")
@Api(value = "convSubjectManage服务", description = "convSubjectManage服务")
public class ConvSubjectController extends BaseController {

    private static final Logger log = LoggerFactory.getLogger(ConvSubjectController.class);

    @Autowired
    private ConvSubjectService convSubjectService;

    /**
     * 获取专题管理列表
     *
     * @param subjectManageListVo 获取专题管理列表 请求入参对象
     * @return ResponseResult<SubjectManageListDto>
     * @author 144637
     */
    @ApiOperation(value = "获取专题管理列表", httpMethod = "POST", notes = "获取专题管理列表")
    @RequestMapping(value = "/getSubjectManageList", method = RequestMethod.POST)
    public ResponseResult<Page> getSubjectManageList(
            @ApiParam(required = true, name = "subjectManageListVo", value = "subjectManageListVo")
            @RequestBody SubjectManageListVo subjectManageListVo) {
        try {
            // 参数校验
            if (subjectManageListVo.getPageNum() == null
                    || subjectManageListVo.getPageSize() == null) {
                return new ResponseResult<>(ConvPhotoExceptionCode.PARAM_IS_NULL);
            }

            Page page = new Page(subjectManageListVo.getPageNum(), subjectManageListVo.getPageSize());

            Page pageResult = convSubjectService.getSubjectManageList(page);

            return ResponseResult.success(pageResult);
        } catch (BusinessException e1) {
            return new ResponseResult<>(AppConstant.FAIL_CODE, e1.getMessage());
        } catch (Exception e) {
            log.error("getSubjectManageList()方法error", e);
            return ResponseResult.fail("", "获取专题管理列表失败");
        }
    }

    /**
     * 新增专题
     *
     * @param addSubjectDto 新增专题 请求入参
     * @return ResponseResult<AddSubjectRequestDto>
     */
    @ApiOperation(value = "新增专题", httpMethod = "POST", notes = "新增专题")
    @RequestMapping(value = "/addSubject", method = RequestMethod.POST)
    public ResponseResult<String> addSubject(
            @ApiParam(required = true, name = "addSubjectDto", value = "addSubjectDto")
            @RequestBody AddSubjectRequestDto addSubjectDto) {
        try {
            if (StringUtils.isAnyBlank(addSubjectDto.getDetailContent()
                    , addSubjectDto.getHint()
                    , addSubjectDto.getPicUrl()
                    , addSubjectDto.getSubjectName())) {
                return new ResponseResult<>(ConvPhotoExceptionCode.PARAM_IS_NULL);
            }
            OrgSessionInfo orgSessionInfo = SessionUtils.getSessionInfo();
            addSubjectDto.setDetailContent(XssEncodeUtils.xssEncode(addSubjectDto.getDetailContent()));
            addSubjectDto.setHint(XssEncodeUtils.xssEncode(addSubjectDto.getHint()));
            int row = convSubjectService.addSubject(addSubjectDto, orgSessionInfo);
            if (row == -1) {
                return new ResponseResult(ConvPhotoExceptionCode.SHELVE_COUNT_OUT_OF_BOUNDS);
            }
            if (row == 0) {
                return new ResponseResult(ConvPhotoExceptionCode.RESULT_IS_NULL);
            }
            return ResponseResult.success("新增专题成功");
        } catch (BusinessException e1) {
            return new ResponseResult<>(AppConstant.FAIL_CODE, e1.getMessage());
        } catch (Exception e) {
            log.error("addSubject()方法error", e);
            return ResponseResult.fail("", "新增专题失败");
        }
    }

    /**
     * 编辑专题
     *
     * @param updateSubjectManageDto 编辑专题 请求入参
     * @return ResponseResult<UpdateSubjectManageDto>
     */
    @ApiOperation(value = "编辑专题", httpMethod = "POST", notes = "编辑专题")
    @RequestMapping(value = "/updateSubject", method = RequestMethod.POST)
    public ResponseResult<String> updateSubject(
            @ApiParam(required = true, name = "updateSubjectManageDto", value = "updateSubjectManageDto")
            @RequestBody UpdateSubjectManageDto updateSubjectManageDto) {
        try {
            if (updateSubjectManageDto.getSubjectId() == null
                    || StringUtils.isAnyBlank(updateSubjectManageDto.getDetailContent()
                    , updateSubjectManageDto.getHint()
                    , updateSubjectManageDto.getPicUrl()
                    , updateSubjectManageDto.getSubjectName())) {
                return new ResponseResult<>(ConvPhotoExceptionCode.PARAM_IS_NULL);
            }
            OrgSessionInfo orgSessionInfo = SessionUtils.getSessionInfo();
            updateSubjectManageDto.setDetailContent(XssEncodeUtils.xssEncode(updateSubjectManageDto.getDetailContent()));
            updateSubjectManageDto.setHint(XssEncodeUtils.xssEncode(updateSubjectManageDto.getHint()));
            int row = convSubjectService.updateSubject(updateSubjectManageDto, orgSessionInfo);
            if (row == -1) {
                return new ResponseResult(ConvPhotoExceptionCode.SHELVE_COUNT_OUT_OF_BOUNDS);
            }
            if (row == 0) {
                return new ResponseResult(ConvPhotoExceptionCode.RESULT_IS_NULL);
            }
            return ResponseResult.success("更新成功");
        } catch (BusinessException e1) {
            return new ResponseResult<>(AppConstant.FAIL_CODE, e1.getMessage());
        } catch (Exception e) {
            log.error("updateSubject()方法error", e);
            return ResponseResult.fail("", "编辑专题失败");
        }
    }

    /**
     * 查询专题详情
     *
     * @param subjectDetailVo 查询专题详情
     * @return ResponseResult<UpdateSubjectManageDto>
     */
    @ApiOperation(value = "查询专题详情", httpMethod = "POST", notes = "查询专题详情")
    @RequestMapping(value = "/querySubjectDetail", method = RequestMethod.POST)
    public ResponseResult<SubjectManageDetailDto> querySubjectDetail(
            @ApiParam(required = true, name = "subjectDetailVo", value = "subjectDetailVo")
            @RequestBody SubjectDetailVo subjectDetailVo) {
        try {
            if (subjectDetailVo.getSubjectId() == null) {
                return new ResponseResult<>(ConvPhotoExceptionCode.PARAM_IS_NULL);
            }
            SubjectManageDetailDto subjectDetailDto = convSubjectService
                    .queryManageSubjectDetail(subjectDetailVo.getSubjectId());
            return ResponseResult.success(subjectDetailDto);
        } catch (BusinessException e1) {
            return new ResponseResult<>(AppConstant.FAIL_CODE, e1.getMessage());
        } catch (Exception e) {
            log.error("updateSubject()方法error", e);
            return ResponseResult.fail("", "编辑专题失败");
        }
    }

    /**
     * 专题上移
     *
     * @param shiftUpSubjectVo 专题上移 请求入参
     * @return ResponseResult<ShiftUpSubjectDto>
     */
    @ApiOperation(value = "专题上移", httpMethod = "POST", notes = "专题上移")
    @RequestMapping(value = "/shiftUpSubject", method = RequestMethod.POST)
    public ResponseResult<String> shiftUpSubject(
            @ApiParam(required = true, name = "shiftUpSubjectVo", value = "shiftUpSubjectVo")
            @RequestBody ShiftUpSubjectVo shiftUpSubjectVo) {
        try {
            //参数校验
            if (StringUtils.isBlank(shiftUpSubjectVo.getSubjectId())) {
                return new ResponseResult<>(ConvPhotoExceptionCode.PARAM_IS_NULL);
            }

            Long subject = Long.valueOf(shiftUpSubjectVo.getSubjectId());

            int row = convSubjectService.shiftUpSubject(subject);

            if (row != 2) {
                return new ResponseResult(ConvPhotoExceptionCode.RESULT_IS_NULL);
            }
            return ResponseResult.success("上移成功");

        } catch (BusinessException e1) {
            return new ResponseResult<>(AppConstant.FAIL_CODE, e1.getMessage());
        } catch (Exception e) {
            log.error("专题上移异常", e);
            return ResponseResult.fail(AppConstant.FAIL_CODE,
                    ConvPhotoExceptionCode.SHIFT_UP_SUBJECT_ERROR.getMessage());
        }

    }

    /**
     * 专题上下架
     *
     * @param shelveOrUnshelveSubjectRequest 专题上下架 请求入参
     */
    @ApiOperation(value = "专题上下架", httpMethod = "POST", notes = "专题上下架")
    @RequestMapping(value = "/shelveOrUnshelveSubject", method = RequestMethod.POST)
    public ResponseResult<String> shelveOrUnshelveSubject(
            @ApiParam(required = true, name = "shelveOrUnshelveSubjectRequest", value = "shelveOrUnshelveSubjectRequest")
            @RequestBody ShelveOrUnshelveSubjectRequest shelveOrUnshelveSubjectRequest) {
        try {
            //参数校验
            if (shelveOrUnshelveSubjectRequest.getSubjectId() == null || shelveOrUnshelveSubjectRequest.getIsShelve() == null) {
                return new ResponseResult<>(ConvPhotoExceptionCode.PARAM_IS_NULL);
            } else if (shelveOrUnshelveSubjectRequest.getIsShelve() >= 2 || shelveOrUnshelveSubjectRequest.getIsShelve() < 0) {
                return new ResponseResult<>(ConvPhotoExceptionCode.PARAM_IS_FAIL);
            }
            //调用service
            Long subjectId = Long.valueOf(shelveOrUnshelveSubjectRequest.getSubjectId());
            Integer IsShelve = shelveOrUnshelveSubjectRequest.getIsShelve();

            int row = convSubjectService.shelveOrUnshelveSubject(subjectId, IsShelve);
            if (row == 1) {

                return ResponseResult.success("成功");
            }
            return ResponseResult.fail(AppConstant.FAIL_CODE, "修改状态失败");
        } catch (BusinessException e1) {
            return new ResponseResult<>(AppConstant.FAIL_CODE, e1.getMessage());
        } catch (Exception e) {
            log.error("shelveOrUnshelveSubject()方法error", e);
            return ResponseResult.fail("", "专题上下架失败");
        }
    }

    /**
     * 上传图片
     *
     * @param uploadInputParam 上传图片 请求入参
     * @return ResponseResult<UploadImgDto>
     */
    @ApiOperation(value = "上传图片", httpMethod = "POST", notes = "上传图片")
    @RequestMapping(value = "/uploadImg", method = RequestMethod.POST)
    public ResponseResult<UploadImgDto> uploadImg(
            @ApiParam(required = true, name = "uploadInputParam", value = "uploadInputParam")
            @RequestBody UploadInputParam uploadInputParam) {
        try {

            if (StringUtils.isAnyBlank(uploadInputParam.getFileStream(), uploadInputParam.getFileType())) {
                return new ResponseResult<>(ConvPhotoExceptionCode.PARAM_IS_NULL);
            }
            UploadImgDto uploadImgDto = convSubjectService.uploadImg(uploadInputParam);

            return ResponseResult.success(uploadImgDto);
        } catch (BusinessException e1) {
            return new ResponseResult<>(AppConstant.FAIL_CODE, e1.getMessage());
        } catch (Exception e) {
            log.error("uploadImg()方法error", e);
            return ResponseResult.fail("", "上传图片失败");
        }
    }

    /**
     * 获取专题名称字典项
     *
     * @return ResponseResult
     */
    @ApiOperation(value = "获取专题名称字典项 ", httpMethod = "POST", notes = "获取专题名称字典项")
    @RequestMapping(value = "/getSubjectNameList", method = RequestMethod.POST)
    public ResponseResult getSubjectNameList() {
        try {
            // 1.参数校验

            // 2. 调用service接口
            List<SubjectNameListDto> subjectNameListDto = convSubjectService.getSubjectNameList();

            // 3. 返回数据
            return ResponseResult.success(subjectNameListDto);
        } catch (BusinessException e1) {
            return new ResponseResult<>(AppConstant.FAIL_CODE, e1.getMessage());
        } catch (Exception e) {
            log.error("getSubjectNameList()方法error", e);
            return ResponseResult.fail("", "getSubjectNameList()方法error");
        }
    }


}